//add by xucr on 20200617 快速链接--APL查询js

//请求  修改传参方式，重写myAjax.js方法
function MaterialReverseAjax2(url,data,successCallback,errCallback){
    // data=encodeURIComponent(data);
    var urlHref1=location.origin
    var urlHref2=location.protocol+"//"+location.hostname;
    if(urlHref1=="http://localhost:63342"){
        url="https://www.easy-mock.com/mock/5bd18ddb18d3a27e0c5b9221/data"+url;
    }else{
        url=urlHref2+"/Windchill/app"+url;
    }
    $.ajax({
        headers: {
            username:"TIANMA",
            password:"TIANMA"
        },
        type:"post",
        url:url,
        dataType:"String",
        data:{
        	params:data
        },
        complete:function(msg){
            var str = msg.responseText;

            str = eval("("+str+")");
            successCallback(str);
        }
    })
}

function MaterialReverseAjax(url,data,successCallback,errCallback){
    var urlHref1=location.origin
    var urlHref2=location.protocol+"//"+location.hostname;
    if(urlHref1=="http://localhost:63342"){
        url="https://www.easy-mock.com/mock/5bd18ddb18d3a27e0c5b9221/data"+url;
    }else{
        url=urlHref2+"/Windchill/app"+url;
    }
    $.ajax({
        headers: {
            username:"TIANMA",
            password:"TIANMA"
        },
        type:"post",
        url:url,
        dataType:"String",
        data:data,
        complete:function(msg){
            var str = msg.responseText;
            str = eval("("+str+")");
            successCallback(str);
        }
    })
}

//构造页面中展示
function showSelect(url, id) {
    MaterialReverseAjax2(url,"",function(str){
        $("#loading").css({
            display:"none"
        });
        if(str.data!=undefined&&str.data.length>0){
        	renderForm1(str.data,id);
        }
    });
}

//构造页面中展示全局枚举列表控件
function showEnumerateSelect(enumerName,id) {
	var p1 = [];
    p1.push({
    	enumerName:enumerName
    })
    MaterialReverseAjax2("/APLController/getEnumeration",JSON.stringify(p1),function(str){
        $("#loading").css({
            display:"none"
        });
        if(str.data!=undefined&&str.data.length>0){
        	renderForm1(str.data,id);
        }
    });
}

//搜索用户
function searchUser() {	
	layui.use(['layer', 'element', 'form'], function () {
        var element = layui.element;
        var layer = layui.layer;
        var form = layui.form;		
		vagueselect($('[name="single_select"]'), 'single');
    });
}

//select模糊搜索
function vagueselect(obj, type) {
	var owner = obj.find('[name="owner"]')
	var select_show = obj.find('[name="select_show"]');
		function getdataList() {
			var userName_input = owner.val();
			
			var p1 = {
				userName:userName_input
			}
			
			//至少输入5位字符才匹配
			if(userName_input == undefined || userName_input.length < 5) {
				return false;
			}
			MaterialReverseAjax("/QuantityController/getUserByName",p1,function(str){
				$("#loading").css({
					display:"none"
				});
				if(str.data!=undefined){
					var html;
					if (str.data.length > 0) {
						//将获得的数据填充到下拉的数据框里
						select_show.children().first().hide().nextAll().remove();
						for (var i = 0; i < str.data.length; i++) {
							html = '<dd lay-value="' + str.data[i].userName + '" name="showUser">' + str.data[i].userFullName + '</dd>';
							select_show.append(html);
						}
						
					} else {
						if (userName_input.length > 0) {
							//如果为搜索到匹配项显示
							select_show.children().first().hide().nextAll().remove();
							html = '<dd lay-value="无" class="layui-select-tips">无该匹配项</dd>';
							select_show.append(html);
						}
						else {
							//如果未输入关键字复原样式
							select_show.children().first().show().nextAll().remove();
						}
					}
				}
			});
		}

	//输入框聚焦事件
	owner.focus(function () {
		obj.find('.layui-form-select').addClass('layui-form-selected');
		getdataList();
	})
	//输入框失去焦点事件
	owner.blur(function () {
		var input_dom = this
		$(document).on('click', function (event) {
			var dom = select_show[0]
			if (event.target !== input_dom && event.target !== dom) {
				obj.find('.layui-form-select').removeClass('layui-form-selected')
			}
		})
	})
	//当按键松开时
	owner.keyup(function () {
		getdataList();
	})
	
	//选择后事件
	obj.delegate('dd', 'click', function () {
		if ($(this).index() > 0) {
			$(this).siblings().removeClass('layui-this')
			$(this).addClass('layui-this')
			owner.val($(this).text())
			obj.find('.layui-form-select').removeClass('layui-form-selected')
		}
	});
}

//构造单选按钮，返回值：{'key':key,'value':value}
function renderForm1(str, select) {
    //var form = layui.form; //高版本建议把括号去掉，有的低版本，需要加()
    var option = "";
    var dat = str;
    for (var i = 0; i < dat.length; i++) {
        option += '<option value="' + dat[i].key + '">' + dat[i].value + '</option>'
    }
    $("#"+select).append(option);
    //form.render('select', 'divAVL');
    $("#loading").css({
        display: "none"
    });
}

//构造【查询、导出】搜索条件
function setSearchCondition() {
    //页面的文本
    var input_all = $('#aplQuantity').find('input');
    var params = {};
    for(var i = 0 ; i < input_all.length; i++) {
    	params[input_all[i].id]=input_all[i].value;
    }
    //页面的选择框
    var select_all = $('#aplQuantity').find('select');
    for(var i = 0 ; i < select_all.length; i++) {
    	params[select_all[i].id]=select_all[i].value;
    }
	//用户组件
	var user = $('#aplQuantity').find('dd[name=showUser]')[0];
	if(undefined != user && "" != params.owner) {
		params["owner"]=user.attributes[0].value
	}
    
    return params;
}